package com.onec.service.customer.service.online;


import com.onec.service.api.dto.request.gateway.BizContent;
import com.onec.service.api.dto.response.customer.MerchantResp;
import com.onec.service.api.enums.ServiceTradeEnum;
import com.onec.service.api.utils.ValidatorUtils;
import com.onec.service.api.utils.group.MerchantRegisterGroup;
import com.onec.service.customer.entity.agent.AgentInfo;
import com.onec.service.customer.entity.merchant.MerchantInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 下游商户注册逻辑：
 * 缺陷：：：
 *
 * @author ONEC
 */
@Service("merchant_register_service")
public class MerchantRegisterService extends BaseOnlineDefaultService {

    private static final Logger logger = LoggerFactory.getLogger(MerchantRegisterService.class);

    /**
     * @param bizContent
     * @return
     */
    @Override
    public MerchantResp execute(BizContent bizContent) {
        // 简单参数验证：
        ValidatorUtils.validateEntity(bizContent, MerchantRegisterGroup.class);
        // 判断商户在机构下的注册上限：
        AgentInfo agentInfo = agentInfoService.getAvailableAgent(bizContent.getAgentNo());


        this.checkRouteCustomer(bizContent);
        // 创建商户信息
        MerchantInfo merchantInfo = merchantInfoService.createMerchant(bizContent, agentInfo);
        // 创建商户账户信息：
        merchantAccountService.createMerchantAccount(merchantInfo);
        // 创建成功，将身份信息配置到机构限制中：
        this.addRouteCustomer(bizContent, ServiceTradeEnum.AGENT_ID_CARD_LIMIT_SERVICE);
        // 将数据提交到需要报备的上游： 异步提交：路由风控获取到可用通道！

        this.registerChannel(bizContent, merchantInfo);

        return this.respRegisterOk(merchantInfo);
    }
}
